﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Mayn.Video
{
    public static class Common
    {
        /// <summary>
        /// 获取指定盘符内存空间
        /// </summary>
        /// <returns></returns>
        public static string appSettingVale(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }


        /// <summary>
        /// 获取指定盘符内存空间
        /// </summary>
        /// <returns></returns>
        public static long getHardDiskFreeSpace(string hardDiskName)
        {
            long freeSpace = new long();
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == hardDiskName)
                {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
                }
            }
            return freeSpace;
        }

        /// <summary>
        /// 视频预览方法
        /// </summary>
        /// <param name="videoPath"></param>
        public static void reviewVideo(string videoPath)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine($@"start {videoPath}&exit");
            p.StandardInput.AutoFlush = true;
            string strOuput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();

        }

        /// <summary>
        /// 获取mac地址
        /// </summary>
        /// <returns></returns>
        public static string macAddress()
        {
            string strMac = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    strMac = mo["MacAddress"].ToString();
                }
            }
            moc = null;
            mc = null;
            return strMac;
        }


        public static string ip4Address(bool orderBy = true)
        {
            string ipv4Address = "";
            if (orderBy)
            {
                ipv4Address = Dns.GetHostEntry(Dns.GetHostName())
                       .AddressList.OrderBy(t => t.ToString())
                       .FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                       ?.ToString();
            }
            else
            {
                ipv4Address = Dns.GetHostEntry(Dns.GetHostName())
                        .AddressList
                        .FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        ?.ToString();
            }

            return ipv4Address;
        }
    }
}
